package cn.appinfo.controller.developer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appinfo.pojo.DevUser;
import cn.appinfo.service.devUser.DevUserService;

@Controller
@RequestMapping("/dev")
public class DevUserController {
	
	@Resource
	DevUserService devUserService;
	
	@RequestMapping("/login")
	public String login(){
		return "devlogin";
	}
	
	@RequestMapping(value="/dologin",method=RequestMethod.POST)
	public String dologin(@RequestParam String devCode,@RequestParam String devPassword,
				HttpServletRequest request,HttpSession session){
		DevUser devUser = devUserService.login(devCode);
		if(null != devUser){
			if(devUser.getDevPassword().equals(devPassword)){//登录成功
				//放入session
				session.setAttribute("devUserSession", devUser);
				//页面跳转（main.jsp）
				return "redirect:/dev/flatform/main";
			}else{
				//页面跳转（login.jsp）带出提示信息--转发
				request.setAttribute("error", "密码不正确");
				return "devlogin";
			}
		}else{
			//页面跳转（login.jsp）带出提示信息--转发
			request.setAttribute("error", "用户名不存在");
			return "devlogin";
		}
	}
	
	@RequestMapping("/flatform/main")
	public String main(HttpSession session){
		if(session.getAttribute("devUserSession")==null){
			return "redirect:/dev/login";
		}
		return "developer/main";
	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		//清除session
		session.removeAttribute("devUserSession");
		return "redirect:/dev/login";
	}
}
